package com.example.myapplication.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.example.myapplication.util.JsonUtil;
import com.example.myapplication.util.OkHttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class AlterUserActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_user);
        EditText editText1= findViewById(R.id.et1);
        EditText editText2= findViewById(R.id.et2);
        EditText editText3= findViewById(R.id.et3);

        Button button1 =findViewById(R.id.button1);
        Button button2 =findViewById(R.id.button2);

        Intent intent = getIntent();
        String user = intent.getStringExtra("user");

        HashMap map = null;

        try {
            map = JsonUtil.getMap(user);
        } catch (JsonProcessingException e) {
            Intent intent1 = new Intent(AlterUserActivity.this,HomeActivity.class);
            startActivity(intent1);
        }
        HashMap finalMap = map;
        editText1.setText(String.valueOf(map.get("name")));
        editText2.setText(String.valueOf(map.get("age")));
        editText3.setText(String.valueOf(map.get("phone")));
        //修改事件
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> map1 = new HashMap<>();
                map1.put("name",editText1.getText().toString());
                map1.put("age",editText2.getText().toString());
                map1.put("phone",editText3.getText().toString());
                map1.put("id",String.valueOf(finalMap.get("id")));

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String json = JsonUtil.getJson(map1);
                            OkHttpUtil.okPost("/updateUser", json, new Callback() {
                                @Override
                                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                                }

                                @Override
                                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                    Intent intent3 = new Intent(AlterUserActivity.this,UserListViewActivity.class);
                                    startActivity(intent3);
                                }
                            });
                        } catch (JsonProcessingException e) {
                            e.printStackTrace();
                        }

                    }
                }).start();
            }
        });
//        删除事件
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(AlterUserActivity.this)
                        .setMessage("确定删除？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                 new Thread(new Runnable() {
                                     @Override
                                     public void run() {
                                         HashMap<Object, Object> mapId = new HashMap<>();
                                         mapId.put("id",String.valueOf(finalMap.get("id")));
                                         try {
                                             String json = JsonUtil.getJson(mapId);
                                             OkHttpUtil.okPost("/deleteId",json , new Callback() {
                                                 @Override
                                                 public void onFailure(@NotNull Call call, @NotNull IOException e) {

                                                 }

                                                 @Override
                                                 public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                                     Intent intent3 = new Intent(AlterUserActivity.this,UserListViewActivity.class);
                                                     startActivity(intent3);
                                                 }
                                             });
                                         } catch (JsonProcessingException e) {
                                             e.printStackTrace();
                                         }


                                     }
                                 }).start();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).show();
            }
        });









    }
}